﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;

namespace ClipPlayer
{
    public partial class DbStatistics
    {
        public static string dbName { get { return "DbStatistics.sdf"; } }
        public static string dbConnectionString { get { return "Data Source=" + dbName + ";Case Sensitive=false;"; } }
        public static bool IsDbOK { get; set; }

        public static void CreateDbIfNeeded()
        {
            IsDbOK = true;
            try
            {
                DbStatistics dbStat = new DbStatistics(DbStatistics.dbConnectionString);
                //
                if (!File.Exists(dbName))
                {
                    SqlCeEngine sce = new SqlCeEngine(dbConnectionString);
                    sce.CreateDatabase();
                    sce.Dispose();
                    //
                    //
                    using (SqlCeConnection con = new SqlCeConnection(dbConnectionString))
                    {
                        con.Open();
                        //using (SqlCeCommand com = new SqlCeCommand("CREATE TABLE played (clipName ntext(255) NOT NULL, nPlayed,int NOT NULL, nSkipped int NOT NULL, nDeleted int NOT NULL);", con))
                        using (SqlCeCommand com = new SqlCeCommand("CREATE TABLE played (clipName nvarchar(256) PRIMARY KEY, nPlayed int, nSkipped int, nDeleted int);", con))
                        {
                            com.ExecuteNonQuery();
                        }
                        con.Close();
                    }
                }
                else
                {
                    var x = from c in dbStat.Played  where c.ClipName=="" select c.NPlayed;
                }
            }
            catch
            {
                IsDbOK = false;
                return;
            }
            //
        }


        private enum Field { Played, Skipped, Deleted };
        private void incrementCounter(string clipName, Field selector)
        {
            if (!IsDbOK) return;
            //
            var x = (from c in this.Played where c.ClipName == clipName select c).FirstOrDefault();
            //B
            if(null==x)
            {
                Played p = new Played() { ClipName = clipName, NPlayed = 0, NSkipped = 0, NDeleted = 0 };
                switch(selector)
                {
                    case Field.Played:
                        p.NPlayed++;
                        break;
                    case Field.Skipped:
                        p.NSkipped++;
                        break;
                    case Field.Deleted:
                        p.NDeleted++;
                        break;
                }
                this.InsertPlayed(p);
                this.SubmitChanges();
            }
            else
            {
                switch (selector)
                {
                    case Field.Played:
                        x.NPlayed++;
                        break;
                    case Field.Skipped:
                        x.NSkipped++;
                        break;
                    case Field.Deleted:
                        x.NDeleted++;
                        break;
                }
                this.SubmitChanges();
            }

        }

        public void incrementPlayed(string clipName)
        {
            incrementCounter(clipName, Field.Played);
        }

        public void incrementSkipped(string clipName)
        {
            incrementCounter(clipName, Field.Skipped);
        }

        public void incrementDeleted(string clipName)
        {
            incrementCounter(clipName, Field.Deleted);
        }


    }//class
}//namespace
